/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.annuaireComp.clientUtils;

import fr.annuaireComp.entities.Competence;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author MiniK
 */
public class CompetenceModel implements Comparable<CompetenceModel> {

    private static Set<String> touchedComp = new HashSet<String>();
    public String id;
    public String competenceMereId;
    public DomaineModel domaine;
    public SavoirModel savoir;
    public String libelle;
    public List<CompetenceModel> competenceFilles;

    public CompetenceModel() {
        competenceFilles = new ArrayList<CompetenceModel>();

    }

    public String getExtendedId() {
        return savoir.id + domaine.id + id;
    }

    public static List<CompetenceModel> buildCompetenceModelFromList(List<Competence> competences) {
        ArrayList<CompetenceModel> compTree = new ArrayList<CompetenceModel>();
        touchedComp.clear();
        for (Competence comp : competences) {
            if (!touchedComp.contains(comp.getIdcompetence().toString())) {
                compTree.add(CompetenceModel.buildCompetenceModel(comp));
            }
        }
        touchedComp.clear();
        Collections.sort(compTree);
        return compTree;
    }

    public static CompetenceModel buildCompetenceModel(Competence competence) {

        return buildCompetenceModel(competence, true);
    }

    public static CompetenceModel buildCompetenceModel(Competence competence, boolean buildCompetenceChildren) {
        //    BrowseCompetenceModel competencesModel = new BrowseCompetenceModel();

        CompetenceModel c = new CompetenceModel();
        c.id = competence.getIdcompetence().toString();
        c.competenceMereId = null;
        if (competence.getCompetenceMere() != null) {
            c.competenceMereId = competence.getCompetenceMere().getIdcompetence().toString();
        }
        touchedComp.add(c.id);//conserve les id deja ajouté dans larbre

        c.libelle = competence.getLibelle();

        String id = competence.getSavoirCompetence().getIdsavoirCompetence().toString();
        String libelle = competence.getSavoirCompetence().getLibelle();
        c.savoir = new SavoirModel(id, libelle);
        id = competence.getDomaineCompetence().getIddomaineCompetence().toString();
        libelle = competence.getDomaineCompetence().getLibelle();
        c.domaine = new DomaineModel(id, libelle);
        //  competencesModel.competencesTree.add(c);
        if (buildCompetenceChildren) {
            for (Competence nestedC : competence.getCompetenceList()) {
                c.competenceFilles.add(buildCompetenceModel(nestedC,true));
            }
        }
        return c;
    }

    @Override
    public int compareTo(CompetenceModel t) {
        return (savoir.id + domaine.id + id).compareTo(t.savoir.id + t.domaine.id + t.id);
    }
}
